package com.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.vo.DormInfoAndRoomieInfoVO;
import com.common.domain.vo.DormPropertyVO;
import com.service.serviceInterface.DormService;
import com.system.entity.Dorm;
import com.system.entity.DormProperty;
import com.system.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 */
@Service
public class DormServiceImpl extends ServiceImpl<DormMapper, Dorm>
        implements DormService {
    @Resource
    private StudentInfoMapper studentInfoMapper;
    @Resource
    private StudentDormMapper studentDormMapper;
    @Resource
    private DormMapper dormMapper;
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private DormPropertyMapper dormPropertyMapper;

    @Override
    public DormInfoAndRoomieInfoVO getDormInfoAndRoomieInfo(long userId) {
        long studentId = studentInfoMapper.selectStudentIdByUserId(userId);
        String dormId = studentDormMapper.selectDormIdByStudentId(studentId);
        Dorm dorm = dormMapper.selectOne(new LambdaQueryWrapper<Dorm>().eq(Dorm::getId, dormId));
        String buildName = buildingMapper.selectNameById(dorm.getBuildingId());
        List<String> nameList = studentDormMapper.selectNameByDormId(dormId);
        List<DormProperty> dormProperties = dormPropertyMapper.selectList(new LambdaQueryWrapper<DormProperty>().eq(DormProperty::getDormId, dormId));
        Set<DormPropertyVO> sets = new HashSet<>();
        for (DormProperty property : dormProperties) {
            DormPropertyVO vo = new DormPropertyVO();
            vo.setPropertyName(property.getPropertyName()).setPropertyNumber(property.getPropertyNumber());
            sets.add(vo);
        }
        DormInfoAndRoomieInfoVO vo = new DormInfoAndRoomieInfoVO();
        vo.setBuildName(buildName).setDormName(dorm.getDormName()).setOccupantsNumber(dorm.getOccupantsNumber()).setNowNumber(dorm.getNowNumber())
                .setNames(nameList).setProperty(sets);
        return vo;
    }
}




